<?php

/**
 * This object represents a CalDAV calendar.
 *
 * A calendar can contain multiple TODO and or Events. These are represented
 * as Sabre_CalDAV_CalendarObject objects.
 * 
   * Copyright © 2010 Jack Cleaver
   * 
   * This plugin provides functionality added by CalDAV (RFC 4791)
   * It implements new reports, and the MKCALENDAR method.
   *
   * This file is part of Slash.
   *
   * Slash is free software: you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation, either version 3 of the License, or
   * (at your option) any later version.
   *
   * Slash is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with Slash.  If not, see <http://www.gnu.org/licenses/>.
 */
class Slash_CalDAV_FSExt_Calendar 
extends Slash_CalDAV_FSExt_Directory 
{


		
		public function __construct($path)
		{
			parent::__construct($path);
			$this->propertyMap[] = "{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set";
			$this->propertyMap[] = "{urn:ietf:params:xml:ns:caldav}calendar-description";
			$this->propertyMap[] = "{urn:ietf:params:xml:ns:caldav}calendar-timezone";
      $this->propertyMap[] = '{http://apple.com/ns/ical/}calendar-order';
      $this->propertyMap[] = '{http://apple.com/ns/ical/}calendar-color';
      $this->propertyMap[] = '{DAV:}displayname';
      //Slash_Debug::debugDump("in Calendar constructor: ", $name);
		}
    /**
     * Creates a new directory
     *
     * We actually block this, as subdirectories are not allowed in calendars.
     * 
     * @param string $name 
     * @return void
     */
    public function createDirectory($name) 
    {
        throw new Sabre_DAV_Exception_MethodNotAllowed('Creating collections in calendar objects is not allowed');
    }

    /**
     * Creates a new file
     *
     * The contents of the new file must be a valid ICalendar string.
     * 
     * @param string $name 
     * @param resource $calendarData 
     * @return void
     */
    public function createFile($name,$calendarData = null) 
    {
        $calendarData = stream_get_contents($calendarData);
        parent::createFile($name, $calendarData);
    }

    /**
     * Renames the calendar. Note that most calendars use the 
     * {DAV:}displayname to display a name to display a name. 
     * 
     * @param string $newName 
     * @return void
     */
    public function setName($newName) 
    {
        throw new Sabre_DAV_Exception_MethodNotAllowed('Renaming calendars is not yet supported');
    }
    
    public function updateCalendar(array $properties) 
    {

        $newValues = array();
        $result = array(
            200 => array(), // Ok
            403 => array(), // Forbidden
            424 => array(), // Failed Dependency
        );

        $hasError = false;

	      //Slash_Debug::debugDump("in updateCalendar, properties: ", $properties);
	      
	      foreach($properties as $propertyName=>$value) 
        {
        	
            // We don't know about this property. 
            if (!in_array($propertyName, $this->propertyMap)) 
            {
            	Slash_Debug::debug("Calendar updateCalendar, Bad property: $propertyName");
                $hasError = true;
                $result[403][$propertyName] = null;
                unset($properties[$propertyName]);
                continue;
            }
        }

        // If there were any errors we need to fail the request
        if ($hasError) 
        {
            // Properties has the remaining properties
            foreach($properties as $propertyName=>$propertyValue) 
            {
                $result[424][$propertyName] = null;
            }

            // Removing unused statuscodes for cleanliness
            foreach($result as $status=>$properties) 
            {
                if (is_array($properties) && count($properties)===0) unset($result[$status]);
            }

            return $result;

        }
        
        if ($this->updateProperties($properties))
        {
	      	Slash_Debug::debug("in updateCalendar, updated properties OK");
        	$result[200] = $properties;
        }
        return $result;
				//return true;
        // Success

    }
    

    /**
     * Returns the last modification date as a unix timestamp.
     * 
     * @return void
     */
    public function getLastModified() 
    {
        return null;
    }

}
